package com.sptpc.learnspring.controller;

import com.sptpc.learnspring.controller.request.NewDrinkRequest;
import com.sptpc.learnspring.model.Drink;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/drink")
public class DrinkController {

    // 前端使用html表单提交数据 Formatter才能生效
//    @PostMapping(value = "/add", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
//    @ResponseBody
//    public Drink addNewDrinkForm(@Valid NewDrinkRequest newDrink) {
//        log.info("创建新饮品{}", newDrink);
//        Drink drink = new Drink();
//        BeanUtils.copyProperties(newDrink, drink);
//        drink.setId(2);
//        log.info("创建成功");
//        return drink;
//    }

    @PostMapping(value = "/add", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
    public Drink addNewDrinkForm(@Valid NewDrinkRequest newDrink, BindingResult bindingResult) {
        // 检查有无校验错误
        if (bindingResult.hasErrors()) {
            // 错误处理
            log.error("创建新饮品失败, case by:");
            for (ObjectError error : bindingResult.getAllErrors()) {
                log.error(error.getDefaultMessage());
            }
            return null;
        } else {
            log.info("创建新饮品{}", newDrink);
            Drink drink = new Drink();
            BeanUtils.copyProperties(newDrink, drink);
            drink.setId(2);
            log.info("创建成功");
            return drink;
        }
    }

    // 前端使用json提交数据
    @PostMapping(value = "/add", consumes = {MediaType.APPLICATION_JSON_VALUE})
    public Drink addNewDrink(@RequestBody @Valid NewDrinkRequest newDrink) {
        log.info("创建新饮品{}", newDrink);
        Drink drink = new Drink();
        BeanUtils.copyProperties(newDrink, drink);
        drink.setId(2);
        log.info("创建成功");
        return drink;
    }

    // 前端上传参数为file
    @PostMapping(value = "/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    public String uploadImage(MultipartFile file) {
        log.info("参数名 {}", file.getName());
        log.info("上传文件名 {}", file.getOriginalFilename());
        log.info("上传文件大小 {}", file.getSize());
        if (!file.isEmpty()) {
            try {
                String fileName = System.currentTimeMillis() + file.getOriginalFilename();
                // 更改为你自己电脑的文件夹路径
                String savePath = "/Users/alexchen/Desktop/upload/";
                // 保存图片位置
                String destFileName = savePath + File.separator + fileName;
                File destFile = new File(destFileName);
                file.transferTo(destFile);

                log.info("上传成功");
                return "上传成功";
            } catch (IOException e) {
                log.info("上传失败:{}", e.toString());
                return "上传失败";
            }
        } else {
            log.info("上传失败");
            return "上传失败";
        }
    }

}
